package com.example.library_demo.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.library_demo.annotation.Auth;
import com.example.library_demo.common.Result;
import com.example.library_demo.dto.book.AddBookDTO;
import com.example.library_demo.dto.book.CheckInBookDTO;
import com.example.library_demo.entity.Book;
import com.example.library_demo.entity.User;
import com.example.library_demo.enums.GlobalResponseResultEnum;
import com.example.library_demo.common.Result;
import com.example.library_demo.service.IBookService;
import com.example.library_demo.service.IUserService;
import com.example.library_demo.vo.book.BookInfoVO;
import com.example.library_demo.vo.book.BookVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Eric
 * @since 2024-11-27
 */
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private IBookService bookService;

    @Autowired
    private IUserService userService;

    @Auth
    @GetMapping
    public Result<IPage<BookVO>> getAllBooks( @RequestParam int pageNum, @RequestParam int pageSize) {
        IPage<BookVO> rst = bookService.pageBookVO(pageNum, pageSize);
        return Result.success(rst);
    }

    @Auth
    @GetMapping("/find")
    public Result<IPage<Book>> find(@RequestParam String text, @RequestParam int pageNum, @RequestParam int pageSize){
        IPage<Book> page = bookService.find(text, pageNum, pageSize);
        return Result.success(page);
    }

    @Auth
    @GetMapping("/{typeId}")
    public Result<List<BookInfoVO>> getBookByType( @PathVariable Long typeId) {
        List<BookInfoVO> rst = bookService.findBookByShelfId(typeId);
        if (!rst.isEmpty()) {
            return Result.success(rst);
        } else {
            return Result.error(GlobalResponseResultEnum.QUERY_DATA_NOT_EXIST_ERROR);
        }
    }

    @Auth
    @GetMapping("/detail/{bookId}")
    public Result<Book> detail( @PathVariable Long bookId) {
        Book book = bookService.getById(bookId);
        if (null != book) {
            return Result.success(book);
        } else {
            return Result.error(GlobalResponseResultEnum.QUERY_DATA_NOT_EXIST_ERROR);
        }
    }

    @Auth
    @PostMapping
    public Result<Book> add( @RequestBody AddBookDTO addBookDTO) {
        Book book = new Book();
        BeanUtils.copyProperties(addBookDTO, book);
        if (bookService.save(book)) {
            return Result.success(book);
        } else {
            return Result.error(GlobalResponseResultEnum.HANDEL_FAIL);
        }
    }

    @Auth
    @GetMapping("/storage")
    public Result<IPage<BookVO>> listStorageBooks( @RequestParam int pageNum, @RequestParam int pageSize) {
        IPage<BookVO> rst = bookService.listStorageBooks(pageNum,  pageSize);
        return Result.success(rst);
    }

    @Auth
    @PutMapping("/checkIn")
    public Result<Void> checkIn( @RequestBody CheckInBookDTO checkInBookDTO){
        bookService.checkIn(checkInBookDTO);
        return Result.success();
    }

    @Auth
    @PutMapping("/checkOut/{bookId}")
    public Result<Void> checkIn(@PathVariable Long bookId){
        bookService.checkOut(bookId);
        return Result.success();
    }
}

